/*
 *  Copyright 2010 mazhao.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package shopping.management.onthefly;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseOnTheFlyAction;
import shopping.model.LoginInfo;

/**

 * User: mazhao
 * Date: 2010/11/20
 * Time: 12:14:04
 */
@org.apache.struts2.convention.annotation.Namespace("/management/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value="shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
    @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
    @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class UserAction extends BaseOnTheFlyAction {

    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    // ======================================
    // for enable or disable user
    // ======================================

    private boolean status ;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Action(value="/management/onthefly/userenable",
        results={
            @Result(name="success", type="json", params = {"ignoreHierarchy", "false"})
        }
    )
    public String enable() {
        if(log.isDebugEnabled()) {
            log.debug("enter management/onthefly/userenable action");
            log.debug("user name " + username);
            log.debug("user status" + status);
        }

        LoginInfo login = new LoginInfo();
        login.setUsername(this.username);

        String message;
        if(status) {
            log.debug("enable user " + this.username);
            login.enable();
            message = "user is enabled";
        } else {
            log.debug("disable user " + this.username);
            login.disable();
            message = "user is disable";
        }

        super.setOnTheFlyStatus(true);
        super.setOnTheFlyMessage(message);

        return SUCCESS;
    }

    // ================================
    //
    // ================================
    private String role;

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Action(value="/management/onthefly/changerole",
        results={
            @Result(name="success", type="json", params = {"ignoreHierarchy", "false"})
        }
    )
    public String changeRole() {
        if(log.isDebugEnabled()) {
            log.debug("enter management/onthefly/changerole action");
            log.debug("user name " + username);
            log.debug("change role to " + role);
        }
        
        LoginInfo login = new LoginInfo();
        login.setUsername(this.username);
        login.setRoleId(this.role);
        login.changeRole();

        return SUCCESS;
    }
}
